
package lanqiao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class Q0609 {
	public static void main(String[] args) throws IOException {
		final int INF = 0x3f3f3f3f;
		int[][] dis = new int[19][19];
		for (int i = 0; i < dis.length; i++) {
			Arrays.fill(dis[i], INF);
			dis[i][i] = 0;
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));

		for (int i = 0; i < 36; i++) {
			String line = br.readLine();
			int a = line.charAt(0) - 'A';
			int b = line.charAt(2) - 'A';
			int c = line.charAt(4) - '0';
			dis[a][b] = c;
			dis[b][a] = c;
		}

		int[] res = new int[19];
		Arrays.fill(res, INF);
		res[0] = 0;
		boolean[] st = new boolean[19];

		for (int i = 0; i < res.length - 1; i++) {
			int t = -1;
			for (int j = 0; j < res.length; j++) {
				if (!st[j] && (t == -1 || res[t] > res[j])) {
					t = j;
				}
			}
			st[t] = true;
			for (int j = 0; j < st.length; j++) {
				res[j] = Math.min(res[j], res[t] + dis[t][j]);
			}
		}

		System.out.println(res[18]);
	}
}

//A B 2
//A C 1
//A D 1
//A E 1
//B G 1
//B J 2
//C D 3
//C F 3
//C G 3
//D E 1
//D G 2
//D H 1
//D I 2
//E H 1
//E I 3
//F G 1
//F J 1
//G K 2
//G I 3
//H I 1
//H L 2
//I M 3
//J S 2
//K N 1
//K P 2
//K L 3
//L M 1
//L R 1
//M Q 1
//M S 1
//M N 2
//N P 1
//O Q 1
//O R 3
//O P 1
//R S 1